package cn.ac.iscas.service.transformer.dameng;

import cn.ac.iscas.base.utils.ParameterUtils;
import cn.ac.iscas.base.utils.SpringUtil;
import cn.ac.iscas.domain.source.DamengSourceColumn;
import cn.ac.iscas.service.queue.dameng.DamengTableQueueService;
import cn.ac.iscas.utils.DamengSqlUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author LJian
 * @version 1.0
 * @description: TODO
 * @date 2022/12/23 18:05
 */
public class DamengBoundaryValueTransformer {
    private final static String ID = "boundary_trans";
    private final static String COLUMN_KEY = "column";
    private final static String MIN_KEY = "min";
    private final static String MAX_KEY = "max";
    private String jobId;
    private String componentId;
    private String frontTableName;
    private List<DamengSourceColumn> frontColumns;

    private String currentTableName;
    private List<DamengSourceColumn> currentColumns;

    public DamengBoundaryValueTransformer(String jobId, String taskId) {
        this.jobId = jobId;
        this.componentId = taskId;
    }

    public DamengBoundaryValueTransformer(String jobId, String taskId, String frontTableName, List<DamengSourceColumn> frontColumns) {
        this.jobId = jobId;
        this.componentId = taskId;
        this.frontTableName = frontTableName;
        this.frontColumns = frontColumns;
        this.currentColumns = frontColumns;
        this.currentTableName = String.format("\"%s\".\"%s_%s\"", jobId, ID, UUID.randomUUID().toString().substring(0,6));
    }


    public DamengBoundaryValueTransformer() {

    }

    public List<String> getSql(Map<String, Object> parameters) {
        List<String> sqls = new ArrayList<>();

        String createTempTableSql = DamengSqlUtils.getCreateTemporaryTableSql(this.currentTableName, this.currentColumns);
        sqls.add(createTempTableSql);

        String minStr = ParameterUtils.getOrDefaultString(parameters, MIN_KEY, null);
        String maxStr = ParameterUtils.getOrDefaultString(parameters, MAX_KEY, null);
        String column = ParameterUtils.getRequiredString(parameters, COLUMN_KEY);
        String selectSql = "";

        if (minStr == null) {
            if (maxStr == null) {
                return null;
            } else {
                selectSql = String.format("( SELECT * FROM %s WHERE %s <= %s )", frontTableName, column, maxStr);
            }
        } else {
            if (maxStr == null) {
                selectSql = String.format("( SELECT * FROM %s WHERE %s >= %s )", frontTableName, column, minStr);
            } else {
                selectSql = String.format("( SELECT * FROM %s WHERE %s >= %s AND %s <= %s)", frontTableName, column, minStr, column, maxStr);
            }
        }

        String insertSql = String.format("INSERT INTO %s %s ", this.currentTableName, selectSql);
        sqls.add(insertSql);
        updateSource();

        return sqls;
    }

    public void updateSource() {
        DamengTableQueueService damengTableQueueService = SpringUtil.getBean(DamengTableQueueService.class);
        Map<String, List<DamengSourceColumn>> tableColumns = new ConcurrentHashMap<>(8);
        tableColumns.put(this.currentTableName, this.currentColumns);
        damengTableQueueService.put(this.jobId, this.componentId, tableColumns);
    }
}
